package com.duomn.mybatis.plugins.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ConfigParse {

	private Map<String, Rule> rules = new HashMap<String, Rule>();
	private List<ShardingTable> tables = new ArrayList<ShardingTable>();
	
	public void parse() {
		InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("mybatis/table_rule.xml");
			 
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder;
			builder = dbf.newDocumentBuilder();
			Document doc = builder.parse(in);
			
			Element root = doc.getDocumentElement();
			if (root == null) return;
			NodeList nodes = root.getChildNodes();
			if (nodes == null) return;
			for (int i = 0; i < nodes.getLength(); i++) {
				Node node = nodes.item(i);
				String nodeName = node.getNodeName();
				if ("rules".equals(nodeName)) {
					
				} else if ("table".equals(nodeName)) {
					
				}
				System.out.println(node);
				System.out.println("pars");
			}
			
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}	 
			 
	}
	
	public static void main(String[] args) {
		ConfigParse cp = new ConfigParse();
		cp.parse();
		System.out.println(cp.tables);
	}
}
